package cn.vshare.controller;

import cn.vshare.entity.PermissionSecond;
import cn.vshare.service.PermissionSecondService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController
public class PermissionSecondController {

    @Resource
    private PermissionSecondService permissionSecondService;


    @RequestMapping(value = "getAllPermissionSecondByIdFirst")
    public List<PermissionSecond> getAllPermissionSecondByIdFirst(String idFirst){
        //System.out.println("idFirst======="+idFirst);
        Map<String,Object>map=new HashMap<>();
        map.put("first_id",idFirst);
        List<PermissionSecond>permissionSecondList=permissionSecondService.queryByConditions1(map);
        return permissionSecondList;
    }


    /**
     * 目的是存到session域,,用于页面之间的数据传递,
     * @param id2PermissionSecond
     * @param session
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "getPermissionSecondById",method = RequestMethod.POST)
    public String getPermissionSecondById(String id2PermissionSecond,
                                          HttpSession session,
                                          HttpServletRequest request,
                                          HttpServletResponse response) throws ServletException, IOException {
        String result="false";
        //System.out.println("id2PermissionSecond====="+id2PermissionSecond);
        PermissionSecond permissionSecond=permissionSecondService.queryById(id2PermissionSecond);
        session.setAttribute("permissionSecond",permissionSecond);//存储到Session域中
        //response.sendRedirect("/h-ui/admin-permission-second-edit.jsp");//重定向
        if (permissionSecond!=null){
            result ="true";
        }else {
            result="flase";
        }
        return result;
    }

    @RequestMapping(value = "getAllPermissionSecondByPage")
    public Map<String,Object> getAllPermissionSecondByPage(int pageNum,int pageSize,String name2Second){

        Map<String,Object>map=new HashMap<>();
        map.put("pageNum",pageNum);//当前页码
        map.put("pageSize",pageSize);//每页显示的最大记录数
        map.put("name",name2Second);//二级权限名称的关键字
        Page page =permissionSecondService.queryByConditions2(map);
        map.put("page",page);//list集合
        map.put("totalRecords",page.getTotal());//总记录数
        map.put("totalPages",page.getPages());//总页数
        return map;
    }

    @RequestMapping(value = "addPermissionSecond",method = RequestMethod.POST)
    public String addPermissionSecond(String permissionSecondName,String permissionSecondDesc,String permissionFirstId){
        //System.out.println("permissionSecondName===="+permissionSecondName);
        //System.out.println("permissionFirstId====="+permissionFirstId);

        PermissionSecond permissionSecond=new PermissionSecond();
        permissionSecond.setId(UUID.randomUUID().toString());
        permissionSecond.setName(permissionSecondName);
        permissionSecond.setDescription(permissionSecondDesc);
        permissionSecond.setIdFirst(permissionFirstId);
        int row=permissionSecondService.addPermissionSecond(permissionSecond);
        if (row==1){
            System.out.println("新增成功");
            return "true";
        }else {
            System.out.println("新增失败");
            return "false";
        }
    }


    @RequestMapping(value = "updatePermissionSecond")
    public String updatePermissionSecond(String update2PermissionFirstId,PermissionSecond permissionSecond){

        System.out.println("update2PermissionFirstId===="+update2PermissionFirstId);
        System.out.println("getId====="+permissionSecond.getId());
        System.out.println("getName====="+permissionSecond.getName());
        System.out.println("getDescription====="+permissionSecond.getDescription());
        System.out.println("getIsForbidden====="+permissionSecond.getIsForbidden());

        permissionSecond.setIdFirst(update2PermissionFirstId);
        int row=permissionSecondService.updatePermissionSecond(permissionSecond);

        if (row==1){
            System.out.println("更新成功");
            return "true";
        }else {
            System.out.println("更新失败");
            return "false";
        }
    }

    @RequestMapping(value = "deletePermissionSecond",method = RequestMethod.POST)
    public String deletePermissionSecond(String delete2PermissionSecondId){
        System.out.println("delete2PermissionSecondId===="+delete2PermissionSecondId);

        int row=permissionSecondService.deletePermissionSecondById(delete2PermissionSecondId);

        if (row==1){
            System.out.println("删除成功");
            return "true";
        }else {
            System.out.println("删除失败");
            return "false";
        }
    }

    //批量删除
    @RequestMapping(value = "delete2Array2PermissionSecond",method = RequestMethod.POST)
    public String delete2Array2PermissionSecond(@RequestBody List<String> delete2Array2PermissionSecondId){
        System.out.println("delete2Array2PermissionSecondId===="+delete2Array2PermissionSecondId);

        List<Integer>list=new ArrayList<>();
        String result="false";
        for (int i=0;i<delete2Array2PermissionSecondId.size();i++){
            int row=permissionSecondService.deletePermissionSecondById(delete2Array2PermissionSecondId.get(i));
            list.add(row);
        }

        //判断是否全部删除,如果删除异常
        //如果删除异常,应该使用事物回滚,需要完善
        for (int i=0;i<list.size();i++){
            if (list.get(i)==1){
                System.out.println("删除成功");
                result="true";
            }else {
                System.out.println("删除失败");
                result="false";
                break;
            }
        }
        return result;

    }


    @RequestMapping(value = "toAdmin_permission_second_edit")
    public ModelAndView toAdmin_permission_second_edit(){
        return new ModelAndView("h-ui/admin_permission_second_edit");
    }






}
